package test;

import java.util.ArrayList;
import java.util.Stack;

/**
 * @program: datastructure
 * @description: 输入一个链表，按链表值从尾到头的顺序返回一个ArrayList。
 * @author: DingZhenYun
 * @create: 2021-11-22 22:41
 **/
class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}

public class Solution3 {
    ArrayList<Integer> arrayList = new ArrayList<>();
    Stack<Integer> stack=new Stack();
    /*
    递归
     */
    public ArrayList<Integer> printov(ListNode listNode) {
        if (listNode != null) {
            this.printov(listNode.next);
            arrayList.add(listNode.val);
        }
        return arrayList;
    }
    /*
    栈
     */
    public ArrayList<Integer> printofStack(ListNode listNode) {
        while (listNode != null) {
            stack.push(listNode.val);
            listNode=listNode.next;
        }
        while(!stack.isEmpty()){
            arrayList.add(stack.pop());
        }
        return arrayList;
    }
    public static void main(String[] args) {
        ListNode list1 = new ListNode(11);
        ListNode list2 = new ListNode(25);
        ListNode list3 = new ListNode(13);
        list1.next = list2;
        list2.next = list3;
        //System.out.println(new Solution3().printov(list1));
        System.out.println(new Solution3().printofStack(list1));
    }

}
